package org.pandaria.war3.w3g.analyseur;

import org.pandaria.war3.log.LogUtil;
import org.pandaria.war3.util.BitsUtil;

public class GameStartRecord {

	public byte[] bytes_gameStartRecord;
	
	/**
	 * 0x0000 |  1 byte   | RecordID - always 0x19
	 */
	public byte[] bytes_recordID = new byte[1];
	
	/**
	 * 0x0001 |  1 word   | number of data bytes following
	 */
	public byte[] bytes_numberDataFollowing = new byte[2];
	
	/**
	 * 0x0003 |  1 byte   | nr of SlotRecords following (== nr of slots on startscreen)
	 */
	public byte[] bytes_numberSlotRecords = new byte[1];
	
	/**
	 * 0x0004 |  n bytes  | nr * SlotRecord (see 4.11)
	 */
	public SlotRecord[] slotRecords;
	public byte[] bytes_slotRecords;
	
	/**
	 *    n+4 |  1 dword  | RandomSeed (see 4.12)
	 */
	public byte[] bytes_randomSeed = new byte[4];
	
	/**
	 *    n+8 |  1 byte   | SelectMode
     *           |   0x00 - team & race selectable (for standard custom games)
     *           |   0x01 - team not selectable
     *           |          (map setting: fixed alliances in WorldEditor)
     *           |   0x03 - team & race not selectable
     *           |          (map setting: fixed player properties in WorldEditor)
     *           |   0x04 - race fixed to random
     *           |          (extended map options: random races selected)
     *           |   0xcc - Automated Match Making (ladder)
	 */
	public byte[] bytes_selectMode = new byte[1];
	
	/**
	 *   n+9 |  1 byte   | StartSpotCount (nr. of start positions in map)
	 */
	public byte[] bytes_startSpotCount = new byte[1];
	
	
	public GameStartRecord(byte[] bytes_gameStartRecord){
		this.bytes_gameStartRecord = bytes_gameStartRecord;
	}
	
	public void parse(){
		
		int offset = 0;
		
		offset = BitsUtil.subArray(this.bytes_gameStartRecord, offset, this.bytes_recordID);
		LogUtil.getLogger().debug("recordID = "+BitsUtil.hexa(this.bytes_recordID));
		
		offset = BitsUtil.subArray(this.bytes_gameStartRecord, offset, this.bytes_numberDataFollowing);
		LogUtil.getLogger().debug("numberDataFollowing = "+BitsUtil.ubits16(this.bytes_numberDataFollowing));
		
		offset = BitsUtil.subArray(this.bytes_gameStartRecord, offset, this.bytes_numberSlotRecords);
		
		int nbSlotRecords = BitsUtil.ubits8(this.bytes_numberSlotRecords);
		LogUtil.getLogger().info("nbSlotRecords = "+nbSlotRecords);
		
		slotRecords = new SlotRecord[nbSlotRecords];
		
		int i = 0;
		
		for (i=0; i<slotRecords.length; i++){
		
			LogUtil.getLogger().debug("slotRecord " + i);
			byte[] bytes_slotRecord = new byte[SlotRecord.SLOT_RECORD_SIZE];
			offset = BitsUtil.subArray(this.bytes_gameStartRecord, offset, bytes_slotRecord);
			
			slotRecords[i] = new SlotRecord(bytes_slotRecord);
			slotRecords[i].parse();
		}
		
		//offset += SlotRecord.SLOT_RECORD_SIZE * nbSlotRecords;
		
		offset = BitsUtil.subArray(this.bytes_gameStartRecord, offset, this.bytes_randomSeed);
		LogUtil.getLogger().debug("randomSeed = "+BitsUtil.ubits32(this.bytes_randomSeed));
		
		offset = BitsUtil.subArray(this.bytes_gameStartRecord, offset, this.bytes_selectMode);
		LogUtil.getLogger().debug("selectMode = "+BitsUtil.hexa(this.bytes_selectMode));
		
		offset = BitsUtil.subArray(this.bytes_gameStartRecord, offset, this.bytes_startSpotCount);
		LogUtil.getLogger().debug("startSpotCount = "+BitsUtil.ubits8(this.bytes_startSpotCount));
		
	}
	
}
